#include "HearbeatDetectionResponsePanel.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include "../../../../../mes/utils/TaskManager.h"

#include "../../../../models/HeartbeatDetectionData.h"
#include "../../../../models/CommonData.h"
#include "../../../../controller/SignalBus.h"

HearbeatDetectionResponsePanel::HearbeatDetectionResponsePanel(QWidget *parent)
    : QWidget{parent}
{
    setupUi();
    connect(SignalBus::instance(),&SignalBus::sendHearbeatStatus,this,&HearbeatDetectionResponsePanel::reciveData);

}

void HearbeatDetectionResponsePanel::initWidgets()
{
    const auto lbResultCode = new QLabel(tr("结果代码"));
    const auto txtResultCode = new QLineEdit();
    const auto lbResultMsg = new QLabel(tr("结果信息"));
    const auto txtResultMsg = new QLineEdit();
    const auto layout = new QGridLayout();

    layout->addWidget(lbResultCode,0,0,1,1);
    layout->addWidget(txtResultCode,0,1,1,1);
    layout->addWidget(lbResultMsg,1,0,1,1);
    layout->addWidget(txtResultMsg,1,1,1,1);

    valueList.insert("resultCode",txtResultCode);
    valueList.insert("resultMsg",txtResultMsg);


    setLayout(layout);

}

void HearbeatDetectionResponsePanel::setupUi()
{
    initWidgets();
}

void HearbeatDetectionResponsePanel::reciveData()
{
    TaskManager task(BaseData<CommonData>::execute(&CommonData::getNetwork));
    QString factoryNo = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getFactoryNo));
    QString transId = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo));
    QString eventUser = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventUser));
    QString machineNo = QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo));
    QString status = QString::fromStdString(BaseData<HeartbeatDetectionData>::execute(&HeartbeatDetectionData::getConnectStatus));

    auto response = task.HeartbeatDetetion(factoryNo,eventUser,machineNo,status);

    if(response!=nullptr)
    {
        valueList["resultCode"]->setText(response->resultCode);
        valueList["resultMsg"]->setText(response->resultMsg);
    }
    else
    {
        valueList["resultCode"]->setText(tr("获取失败"));
        valueList["resultMsg"]->setText(tr("获取失败"));
    }
    BaseData<CommonData>::execute(&CommonData::getNetwork)->cancelTask(task.TaskId(task.HeartbeatDetection()));
}
